package com.zd.lib.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

public class NetworkUtils {
	/**
	 * 检查设备是否连接网络，未连接则提示进行网络设置
	 * 
	 * @author 丁晓宇
	 * 
	 * @QQ 185454510
	 * 
	 * @mail dev_dxy@163.com
	 */
	public static boolean checkNetworkAvailable(final Context context) {
		boolean retVal = isNetworkAvailable(context);
		if (!retVal) {
			AlertDialog.Builder builders = new AlertDialog.Builder(context);
			builders.setTitle("抱歉，网络连接失败，是否进行网络设置？");
			builders.setPositiveButton("设置", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// 进入无线网络配置界面
					context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
				}
			});
			builders.setNegativeButton("稍后", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					try{
						dialog.dismiss();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			builders.show();
		}
		return retVal;
	}

	/**
	 * 检查设备是否连接网络
	 * 
	 * @param context
	 * 
	 * @return 联网状态
	 */
	public static boolean isNetworkAvailable(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

}
